#include "WeaponFactory.h"
#include "CNumber.h"
#include "BulletFactory.h"
#include "BulletsBank.h"


WeaponFactory::WeaponFactory(void)
{
	_id = NULL;
	_bulletData = NULL;
	_extraData = NULL;
	_timeDelay = 0;
}


WeaponFactory::~WeaponFactory(void)
{
	CC_SAFE_RELEASE(_id);
	CC_SAFE_RELEASE(_bulletData);
	CC_SAFE_RELEASE(_extraData);
}

bool WeaponFactory::init()
{
	setBulletData(CCArray::create());
	return true;
}

void WeaponFactory::parseXml( xml_node node )
{
	char* id = (char*)node.child("Id").text().as_string();
	float timeDelay = node.child("TimeDelay").text().as_float();

	xml_node extra = node.child("Extra");
	if (extra != xml_node(NULL))
	{
		setExtraData(CCDictionary::create());
		float timeWave = extra.child("WaveTimeDelay").text().as_float();
		int numShoot = extra.child("NumberShot").text().as_int();

		_extraData->setObject(CCNumber<float>::create(timeWave), "TimeWaveDelay");
		_extraData->setObject(CCNumber<int>::create(numShoot), "NumberShot");
	}
	

	xml_node bullets = node.child("Bullets");
	for (xml_node j = bullets.child("Bullet"); j; j = j.next_sibling("Bullet"))
	{
		char* bId = (char*)j.child("Id").text().as_string();
		int bPosX = j.child("PositionX").text().as_int();
		int bPosY = j.child("PositionY").text().as_int();
		int bVelX = j.child("VelocityX").text().as_int();
		int bVelY = j.child("VelocityY").text().as_int();
		float bScaleX = j.child("ScaleX").text().as_float();
		float bScaleY = j.child("ScaleY").text().as_float();
		int bDame = j.child("Damage").text().as_int();

		CCDictionary* bullet = CCDictionary::create();
		bullet->setObject(ccs(bId), "Id");
		
		bullet->setObject(CCNumber<CCPoint>::create(ccp(bPosX, bPosY)), "Position");

		bullet->setObject(CCNumber<CCPoint>::create(ccp(bVelX, bVelY)), "Velocity");

		bullet->setObject(CCNumber<CCPoint>::create(ccp(bScaleX, bScaleY)), "Scale");

		bullet->setObject(CCNumber<int>::create(bDame), "Damage");

		_bulletData->addObject(bullet);
	}

	setId(ccs(id));
	setTimeDelay(timeDelay);
}

Weapon* WeaponFactory::createWeapon()
{
	Weapon* weapon = Weapon::create();
	weapon->setId(_id);
	weapon->setTimeDelay(_timeDelay);
	weapon->setBulletData(_bulletData);
	weapon->setExtraData(_extraData);

	return weapon;
}
